package DBConnection;

import java.util.List;

import model.entities.Predictions;

import org.hibernate.HibernateException;

public class PredictionsDB extends HibernateUtil implements
		ConnectDB<Predictions, Boolean, Integer> {

	@Override
	public Boolean add(Predictions Predictions) {
		try {
			if (Predictions != null) {
				this.beginTranSaction();
				this.getCurrentSession().save(Predictions);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Boolean delete(Integer ID) {
		Predictions Predictions = find(ID);
		try {
			if (Predictions != null) {
				this.beginTranSaction();
				this.getCurrentSession().delete(Predictions);
				this.commitTransaction();
			}
		} catch (HibernateException e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Predictions> findAll() {
		try {
			this.beginTranSaction();
			return this.getCurrentSession()
					.createCriteria(Predictions.class).list();
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	@Override
	public Boolean update(Predictions Predictions) {
		try {
			if (Predictions != null) {
				this.beginTranSaction();
				this.getCurrentSession().update(Predictions);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Predictions find(Integer ID) {
		try {
			this.beginTranSaction();
			return (Predictions) this.getCurrentSession().get(
					Predictions.class, ID);
		} catch (Exception e) {
			throw e;
		}
	}
	public static void main(String[] args) {
		List<Predictions> ll = new PredictionsDB().findAll();
		System.out.println(ll.size());
		System.out.println(new PredictionsDB().find(1));
	}

}
